﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Project_Udrinkwater.PagesAdmin
{
    public partial class WithDrawEmployee : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (ASPxComboBox1.Value !="" && ASPxDateEdit1.Value !="" && tb_Number.Text!="")
                {
                 int Num = Convert.ToInt32(tb_Number.Text);
                 string [] Employee  = ASPxComboBox1.Value.ToString().Split(' ');
                 string Emp = Employee[0];
                    if (chboxlist.SelectedValues.ToString() == "0")
                    {
                        string Detial = chboxlist.SelectedItem.Text;
                        Insertss(Emp,Detial);

                        GV_ShowWithDraw.DataBind();
                        tb_Number.Text = "";
                        ASPxComboBox1.Value = "";
                        ASPxDateEdit1.Value = "";
                        Response.Write(@"<script language='javascript'>alert('ทำรายการเรียบร้อยแล้ว')</script>");
                    }
                    else
                    {
                        if (tb_Detial.Text != "")
                        {
                             string Detial = tb_Detial.Text;
                            Insertss(Emp, Detial);

                            GV_ShowWithDraw.DataBind();
                            tb_Number.Text = "";
                            ASPxComboBox1.Value = "";
                            ASPxDateEdit1.Value = "";
                            Response.Write(@"<script language='javascript'>alert('ทำรายการเรียบร้อยแล้ว')</script>");
                        }
                        else
                        {
                            Response.Write(@"<script language='javascript'>alert('กรอกข้อมูลไม่ครบถ้วน')</script>"); 
                        }
                       
                    }
                  
                   
                }   
            }
            catch (Exception)
            {
                Response.Write(@"<script language='javascript'>alert('กรอกข้อมูลไม่ถูกต้อง')</script>");
                
            }
           
           
        }
        public void Insertss(string Emp,string Detial)
        {
            string sqltext = @"insert into [TB_withdraw] 
                                   ( [Employee_ID]
                                    ,[Date_withdraw]
                                    ,[Detial]
                                    ,[Num_withdraw]
                                    ,[signature_Admin])
                                   values (@Empoyee_ID,@Date_withdraw,@Detial,@Num_withdraw,@admin)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@Empoyee_ID",Emp );
            sqlcomm.Parameters.AddWithValue("@Date_withdraw", Convert.ToDateTime(ASPxDateEdit1.Value));
            sqlcomm.Parameters.AddWithValue("@Detial", Convert.ToDateTime(ASPxDateEdit1.Value));
            sqlcomm.Parameters.AddWithValue("@Num_withdraw",Convert.ToInt32(tb_Number.Text) );
            sqlcomm.Parameters.AddWithValue("@admin",Session["User"].ToString());
            
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }

        protected void bt_ExportExcel_Click(object sender, EventArgs e)
        {
            GV_ExportPay.WriteXlsToResponse();
        }

        protected void bt_ExportPDF_Click(object sender, EventArgs e)
        {
            GV_ExportPay.WritePdfToResponse();
        }
     
    }
}